<template>
  <a-modal
    :visible="visible"
    title="新增公告"
    cancelText="取消"
    okText="提交"
    width="60%"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-row>
        <a-col :span="8">
          <a-form-item ref="title" label="联系人姓名" name="title">
            <a-input v-model:value="formState.name" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item ref="content" label="所属部门" name="content">
            <a-input v-model:value="formState.department" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item ref="content" label="职位" name="content">
            <a-input v-model:value="formState.post" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item ref="content" label="所属学校" name="content">
            <a-input v-model:value="formState.universe" readonly>
              <template #addonAfter>
                <div class="primary-color cursor-pointer" @click="openDialog('universityVisible')">选择</div>
              </template>
            </a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item ref="content" label="负责人" name="content">
            <a-input v-model:value="formState.charge">
              <template #addonAfter>
                <div class="primary-color cursor-pointer" @click="openDialog('chargeVisible')">选择</div>
              </template>
            </a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item ref="content" label="电话" name="content">
            <a-input v-model:value="formState.phone" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item ref="content" label="Email" name="content">
            <a-input v-model:value="formState.email" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item ref="content" label="性别" name="content">
            <a-input v-model:value="formState.sex" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item ref="content" label="状态" name="content">
            <a-input v-model:value="formState.status" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item ref="content" label="办公地址" name="content">
            <a-input v-model:value="formState.address" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <university :visible="formState.universityVisible"
                @selectUniversity="selectUniversity"
                @close="closeDialog"
    />
    <charge :visible="formState.chargeVisible"
            @selectUniversity="selectCharge"
            @close="closeDialog"
    />
  </a-modal>
</template>
<script>
import { message } from 'ant-design-vue';
import { defineComponent, reactive, ref, toRaw } from "vue";
import university from "./university";
import charge from "./charge";
export default defineComponent({
  components: {
    university,
    charge
  },
  props: {
    visible: {
      type: Boolean,
    },
  },
  emit: ["close"],
  setup(props, context) {

    const formRef = ref();

    const formState = reactive({
      name: '',
      department: '',
      post: '',
      universe: '',
      charge: '',
      phone: '',
      email: '',
      sex: '',
      status: '',
      address: '',
      universityVisible: false,
      chargeVisible: false
    });

    const formRules = {
      name: [ { required: true, message: '请输入联系人姓名', trigger: 'blur'} ],
      department: [ { required: true, message: '所属部门', trigger: 'blur'} ]
    };

    const saveKey = "save";

    const submit = (e) => {
      message.loading({ content: '提交中...', key: saveKey });
      console.log(e, 'e')
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };

    const openDialog = (type) => {
      formState[type] = true
    }

    const closeDialog = (type) => {
      formState[type] = false
    }

    const selectUniversity = (value) => {
      console.log(value, 'value')
    }

    const selectCharge = () => {

    }

    return {
      submit,
      cancel,
      formRef,
      formState,
      formRules,
      selectUniversity,
      selectCharge,
      openDialog,
      closeDialog,

      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
    };
  },
});
</script>
